package com.yingxin.yxerp.module.sys.controller;

import com.yingxin.yxerp.module.sys.entity.WasteName;
import com.yingxin.yxerp.module.sys.entity.builder.WasteNameBuilder;
import com.yingxin.yxerp.module.sys.service.IWasteNameService;
import com.yingxin.yxerp.module.sys.vo.req.*;
import com.yingxin.yxerp.module.sys.vo.resp.WasteNameRespVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.List;

/**
 * 废料名称 前端控制器
 * @author yuebao
 * @since 2025-03-31
 */

@RestController
@RequestMapping("/sys/waste/name")
public class WasteNameController {

    @Autowired
    private IWasteNameService wasteNameService;

    @ApiOperation(value = "查询列表-全部")
    @PostMapping("/list")
    public List<WasteNameRespVo> list(@RequestBody WasteNameQueryVo queryVo) {
        return wasteNameService.listVo( queryVo );
    }

    @ApiOperation(value = "根据id查询")
    @GetMapping("/{id}")
    public WasteNameRespVo get(@PathVariable("id") String id) {
        return wasteNameService.getVo( id );
    }

    @PreAuthorize("hasAuthority('system:waste:name:save')")
    @ApiOperation(value = "保存")
    @PostMapping("/save")
    public String save(@RequestBody @Valid WasteNameCreateVo createVo) {
        WasteName wasteName = WasteNameBuilder.build( createVo );
        wasteNameService.create( wasteName );
        return wasteName.getId();
    }

    @PreAuthorize("hasAuthority('system:waste:name:delete')")
    @ApiOperation(value = "删除")
    @DeleteMapping("/{id}")
    public void delete(@PathVariable String id) {
        wasteNameService.delete( id );
    }

    @PreAuthorize("hasAuthority('system:waste:name:update')")
    @ApiOperation(value = "编辑")
    @PutMapping("/update")
    public void update(@RequestBody @Valid WasteNameUpdateVo updateVo) {
        WasteName wasteName = new WasteName();
        BeanUtils.copyProperties( updateVo, wasteName );
        wasteNameService.update( wasteName );
    }

}
